﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using THL.DataAccess;
using THL.DataAccess.Models;

namespace ConsoleApplication1
{   
    class Program
    {
        static Regex DataRegex = new Regex(@"((-|)\d+.\d+)(%|亿|万|)");
        static void Main(string[] args)
        {
            DateTime dt = DateTime.Now;
            Console.WriteLine(dt.ToString());
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.GetEncoding("GB2312");
            List<StockRecord> stockList = DataProvider.Instance.GetStockList();
            //List<ZiJingLiuXiangRecord> lxTable = new List<ZiJingLiuXiangRecord>();
            Int32 j = 0;
            Int32 db_result;
            foreach (StockRecord stock in stockList)
            {                
                HtmlDocument html = web.Load("http://data.eastmoney.com/zjlx/" + stock.ZQDM + ".html", "GET");
                HtmlNode node = html.GetElementbyId("dt_1");//content_zjlxtable"); 
                if (node == null) continue;
                var tbody = node.SelectSingleNode("./tbody");
                foreach (var tr in tbody.ChildNodes)
                {
                    if (tr.Name.ToLower().Equals("tr"))
                    {
                        var a = tr.SelectSingleNode(tr.XPath + "/td[1]").Attributes;
                        if (a.Count > 0)
                        {
                            if (a[0].Name == "colspan" && a[0].Value == "13")
                            {
                                break;
                            }
                        }
                        Decimal zhuli_jlr, x_jlr, l_jlr, m_jlr, s_jlr, drsp;
                        Double zhuli_jzb, x_jzb, l_jzb, m_jzb, s_jzb, zdf;                        
                        DateTime jyrq = DateTime.Now;
                        
                        DateTime.TryParse(tr.SelectSingleNode(tr.XPath + "/td[1]").InnerText.Trim(), out jyrq);
                        Decimal.TryParse(tr.SelectSingleNode(tr.XPath + "/td[2]").InnerText.Trim(), out drsp);

                        Match zdf_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[3]").InnerText.Trim());
                        Match z_jlr_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[4]").InnerText.Trim());
                        Match z_jzb_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[5]").InnerText.Trim());
                        Match x_jlr_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[6]").InnerText.Trim());
                        Match x_jzb_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[7]").InnerText.Trim());
                        Match l_jlr_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[8]").InnerText.Trim());
                        Match l_jzb_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[9]").InnerText.Trim());
                        Match m_jlr_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[10]").InnerText.Trim());
                        Match m_jzb_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[11]").InnerText.Trim());
                        Match s_jlr_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[12]").InnerText.Trim());
                        Match s_jzb_match = DataRegex.Match(tr.SelectSingleNode(tr.XPath + "/td[13]").InnerText.Trim());

                        Double.TryParse(zdf_match.Groups[1].Value, out zdf);
                        Decimal.TryParse(z_jlr_match.Groups[1].Value, out zhuli_jlr);
                        Double.TryParse(z_jzb_match.Groups[1].Value, out zhuli_jzb);
                        Decimal.TryParse(x_jlr_match.Groups[1].Value, out x_jlr);
                        Double.TryParse(x_jzb_match.Groups[1].Value, out x_jzb);
                        Decimal.TryParse(l_jlr_match.Groups[1].Value, out l_jlr);
                        Double.TryParse(l_jzb_match.Groups[1].Value, out l_jzb);
                        Decimal.TryParse(m_jlr_match.Groups[1].Value, out m_jlr);
                        Double.TryParse(m_jzb_match.Groups[1].Value, out m_jzb);
                        Decimal.TryParse(s_jlr_match.Groups[1].Value, out s_jlr);
                        Double.TryParse(s_jzb_match.Groups[1].Value, out s_jzb);

                        zhuli_jlr = z_jlr_match.Groups[3].Value == "亿" ? zhuli_jlr * 10000 : zhuli_jlr;
                        x_jlr = x_jlr_match.Groups[3].Value == "亿" ? x_jlr * 10000 : x_jlr;
                        l_jlr = l_jlr_match.Groups[3].Value == "亿" ? l_jlr * 10000 : l_jlr;
                        m_jlr = m_jlr_match.Groups[3].Value == "亿" ? m_jlr * 10000 : m_jlr;
                        s_jlr = s_jlr_match.Groups[3].Value == "亿" ? s_jlr * 10000 : s_jlr;

                        ZiJingLiuXiangRecord newZJLX = new ZiJingLiuXiangRecord
                        {
                            Stock_Id = stock.Id,
                            DRSP = drsp,
                            Date = jyrq,
                            ZDF = zdf,
                            ZhuLi_JingLiuRu = zhuli_jlr,
                            ZhuLi_JingZhanBi = zhuli_jzb,
                            ExtraLarge_JingLiuRu = x_jlr,
                            ExtraLarge_JingZhanBi = x_jzb,
                            Large_JingLiuRu = l_jlr,
                            Large_JingZhanBi = l_jzb,
                            Medium_JingLiuRu = m_jlr,
                            Medium_JingZhanBi = m_jzb,
                            Small_JingLiuRu = s_jlr,
                            Small_JingZhanBi = s_jzb,
                            //2,002533,金杯电工,9.52,-0.63,-1018.29,-5.03,-102.17,-0.50,-916.12,-4.53,-221.20,-1.09,1239.49,6.12,2015-11-10 15:00:00
                            OriginalData = String.Format(
                                "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15}",
                                stock.SCDM == "sh" ? "1" : "2",
                                stock.ZQDM, stock.ZQMC, drsp.ToString(), zdf.ToString(),
                                zhuli_jlr.ToString(), zhuli_jzb.ToString(),
                                x_jlr.ToString(), x_jzb.ToString(),
                                l_jlr.ToString(), l_jzb.ToString(),
                                m_jlr.ToString(), m_jzb.ToString(),
                                s_jlr.ToString(), s_jzb.ToString(),
                                jyrq.ToString("yyyy-MM-dd 00:00:00")
                            )
                        };
                        //newZJLX.ZhuLi_JingLiuRu_Unit = newZJLX.ExtraLarge_JingLiuRu_Unit = newZJLX.Large_JingLiuRu_Unit = newZJLX.Medium_JingLiuRu_Unit = newZJLX.Small_JingLiuRu_Unit = "万";
                        try
                        {
                            ZiJingLiuXiangRecord zjlx = DataProvider.Instance.GetZJLX(stock.Id, newZJLX.Date);
                            HQRecord hq = DataProvider.Instance.GetHQ(stock.Id, newZJLX.Date);
                            if (zjlx == null)
                            {
                                db_result = DataProvider.Instance.CreateZJLX(newZJLX);
                            }
                            else
                            {
                                if (String.IsNullOrEmpty(zjlx.OriginalData))
                                {
                                    db_result = DataProvider.Instance.UpdateZJLX(newZJLX, zjlx);
                                }
                            }
                            if (hq == null)
                            {
                                db_result = DataProvider.Instance.CreateHQ(new HQRecord { ZDF = newZJLX.ZDF, DRSP = newZJLX.DRSP, Stock_Id = newZJLX.Stock_Id, Date = newZJLX.Date });
                            }
                            else if (hq.ZDF == 0 || hq.DRSP == 0)
                            {
                                db_result = DataProvider.Instance.UpdateHQ(new HQRecord { ZDF = newZJLX.ZDF, DRSP = newZJLX.DRSP, Stock_Id = hq.Stock_Id, Date = hq.Date });
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.InnerException.Message);
                        }
                        //lxTable.Add(newZJLX);
                    }
                }
                ++j;
                if (j % 10 == 0 || j == 1 || j == stockList.Count)
                {
                    Console.WriteLine(j + " {" + stock.ZQMC + "," + stock.ZQDM + "," + stock.Id + "}");
                }
            }
            var time = DateTime.Now.Subtract(dt).TotalMinutes;
            Console.WriteLine(time);
            Console.ReadLine();
        }
    }
}
